package com.study.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.study.annotation.AuthCheck;
import com.study.common.BaseResponse;
import com.study.common.DeleteRequest;
import com.study.common.ResultUtils;
import com.study.constant.UserConstant;
import com.study.exception.ErrorCode;
import com.study.exception.ThrowUtils;
import com.study.model.dto.note.LearningNoteAddRequest;
import com.study.model.dto.note.LearningNoteQueryRequest;
import com.study.model.dto.note.LearningNoteUpdateRequest;
import com.study.model.dto.topic.TopicAddRequest;
import com.study.model.dto.topic.TopicQueryRequest;
import com.study.model.dto.topic.TopicUpdateRequest;
import com.study.model.entity.LearningNote;
import com.study.model.entity.Topic;
import com.study.model.vo.LearningNoteVO;
import com.study.model.vo.TopicVO;
import com.study.service.LearningNoteService;
import com.study.service.TopicService;
import com.study.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 话题控制器
 */
@RestController
@RequestMapping("/topic")
public class TopicController {


    @Resource
    private TopicService topicService;

    /**
     * 创建话题
     */
    @PostMapping("/add")
    public BaseResponse<Long> addTopic(@RequestBody TopicAddRequest topicAddRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(topicAddRequest == null, ErrorCode.PARAMS_ERROR);
        return ResultUtils.success(topicService.createLearningNote(topicAddRequest,request));
    }

    /**
     * 根据 id 获取话题（仅管理员）
     */
    @GetMapping("/get")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Topic> getTopicById(long id) {
        ThrowUtils.throwIf(id <= 0, ErrorCode.PARAMS_ERROR);
        Topic note = topicService.getById(id);
        ThrowUtils.throwIf(note == null, ErrorCode.NOT_FOUND_ERROR);
        return ResultUtils.success(note);
    }

    /**
     * 根据 id 获取包装类
     */
    @GetMapping("/get/vo")
    public BaseResponse<TopicVO> getTopicVOById(long id) {
        ThrowUtils.throwIf(id <= 0, ErrorCode.PARAMS_ERROR);
        TopicVO topicVO=topicService.getTopicVOById(id);
        return ResultUtils.success(topicVO);
    }


    /**
     * 删除话题
     */
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteTopic(@RequestBody DeleteRequest deleteRequest) {
        ThrowUtils.throwIf(deleteRequest == null || deleteRequest.getId() <= 0, ErrorCode.PARAMS_ERROR);
        return ResultUtils.success(topicService.removeById(deleteRequest.getId()));

    }

    /**
     * 更新话题
     */
    @PostMapping("/update")
    public BaseResponse<Boolean> updateTopic(@RequestBody TopicUpdateRequest topicUpdateRequest) {
        ThrowUtils.throwIf(topicUpdateRequest == null || topicUpdateRequest.getId() <= 0, ErrorCode.PARAMS_ERROR);
        return ResultUtils.success(topicService.updateTopic(topicUpdateRequest));

    }

    /**
     * 分页获取话题封装列表（仅管理员）
     *
     * @param topicQueryRequest 查询请求参数
     */
    @PostMapping("/list/page/vo")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Page<TopicVO>> listTopicVOByPage(@RequestBody TopicQueryRequest topicQueryRequest) {
        ThrowUtils.throwIf(topicQueryRequest == null, ErrorCode.PARAMS_ERROR);
        return ResultUtils.success(topicService.listTopicVOByPage(topicQueryRequest));
    }
    /**
     * 分页获取话题封装列表（仅管理员）
     *
     * @param topicQueryRequest 查询请求参数
     */
    @PostMapping("/list/page/vo/user")
    public BaseResponse<Page<TopicVO>> listTopicVOByPageUser(@RequestBody TopicQueryRequest topicQueryRequest) {
        ThrowUtils.throwIf(topicQueryRequest == null, ErrorCode.PARAMS_ERROR);
        return ResultUtils.success(topicService.listTopicVOByPage(topicQueryRequest));
    }

}
